﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10_UniqueInOrder
{
    public static class Kata
    {
        public static IEnumerable<T> UniqueInOrder<T>(IEnumerable<T> iterable)
        {
            //your code here...
            var retList = new List<T>();
            foreach (var element in iterable) if (!element.Equals(retList.LastOrDefault())) retList.Add(element);
            return retList;

            //2.第二种高赞回答
            //T previous = default(T);
            //foreach (T current in iterable)
            //{
            //    if (!current.Equals(previous))
            //    {
            //        previous = current;
            //        yield return current;
            //    }
            //}

            //3.对IEnumerable接口的深入了解
            //var e = iterable.GetEnumerator();
            //if (e.MoveNext())
            //{
            //    var c = e.Current;
            //    while (e.MoveNext())
            //    {
            //        if (!e.Current.Equals(c)) yield return c;
            //        c = e.Current;
            //    }
            //    yield return c;
            //}

            //4.利用Linq快速实现
            //return iterable.Where((x, i) => i == 0 || !Equals(x, iterable.ElementAt(i - 1)));
        }
    }
}
